package cn.cjlmonster.demo02.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.utils.DpUtils;

/**
 * cn.cjlmonster.demo02.widget
 *
 * @author cjl
 * @project Demo02
 * @date 2021/12/22 9:04 下午
 */
public class RingGradientView extends View {

    public RingGradientView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private Paint paint;

    private int colorCenter;
    private int colorEdge;
    private int ringWidth;

    private void getAttrs(Context context, @Nullable AttributeSet attrs) {

        colorCenter = Color.parseColor("#000055");
        colorEdge = Color.parseColor("#FFFFFF");
        ringWidth = DpUtils.dpToPx(getContext(), 30);

        if (attrs == null) return;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RingGradientView);
        colorCenter = a.getColor(R.styleable.RingGradientView_colorCenter, colorCenter);
        colorEdge = a.getColor(R.styleable.RingGradientView_colorEdge, colorEdge);
        ringWidth = a.getDimensionPixelOffset(R.styleable.RingGradientView_ringWidth, ringWidth);
        a.recycle();
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        getAttrs(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        RectF rectf = new RectF(0, 0, width, height);

        paint.setStrokeWidth(ringWidth);

        float cx = width / 2f;
        float cy = height / 2f;
//        float r = width / 2f + ringWidth / 5f;
        float r = width / 2f + ringWidth * (progress / 100f);

        float p = 1f - (ringWidth / cx);

        int[] color = {
                colorCenter,
                colorEdge
        };
        float[] position = {p, 1f};

//        RadialGradient radialGradient = new RadialGradient(
//                cx, cy, r, Color.BLUE, Color.WHITE, Shader.TileMode.REPEAT
//        );
        RadialGradient radialGradient = new RadialGradient(
                cx, cy, r, color, position, Shader.TileMode.CLAMP
        );
        RectF rectF = new RectF();
        rectF.left = rectf.left + ringWidth / 2f + 5;
        rectF.right = rectf.right - ringWidth / 2f - 5;
        rectF.top = rectf.top + ringWidth / 2f + 5;
        rectF.bottom = rectf.bottom - ringWidth / 2f - 5;
        paint.setShader(radialGradient);
        canvas.drawArc(rectF, 135, 270, false, paint);
    }


//    public void setColor(@IntRange(from = 0, to = 100) int progress) {
//        int alpha = progress + 155;
//        int red = Color.red(colorCenter);
//        int green = Color.green(colorCenter);
//        int blue = Color.blue(colorCenter);
//        colorCenter = Color.argb(alpha, red, green, blue);
//        invalidate();
//    }

    private int progress;

    public void setColor(int progress) {
        this.progress = progress;
        invalidate();
    }

}
